import axios from 'axios'
import { showToast } from 'vant'

const instance = axios.create({
  baseURL: import.meta.env.VITE_BASE_API,
  timeout: 10000
})

instance.interceptors.request.use(
  (config) => {
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

instance.interceptors.response.use(
  (res) => {
    return res.data
  },
  (err) => {
    showToast(err.response.data.msg)
    return Promise.reject(err)
  }
)

const request = (url, method = 'get', data) =>
  instance.request({
    url,
    method,
    [method.toLocaleLowerCase() === 'get' ? 'params' : 'data']: data
  })
export default request
